package com.kreedx.kdemo.module;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.kreedx.kdemo.R;
import com.kreedx.kdemo.module.contract.HomeContract;
import com.kreedx.kdemo.module.presenter.HomePresenter;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUIWrapContentListView;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.xujiaji.xmvp.view.base.v4.XBaseFragment;

import static com.kreedx.kdemo.common.FragmentFactory.listStrings;

public class HomeFragment extends XBaseFragment<HomePresenter> implements HomeContract.View {

    @BindView(R.id.top_bar)
    QMUITopBar topBar;
    @BindView(R.id.list_view)
    QMUIWrapContentListView listView;
    @BindView(R.id.refresh_layout)
    QMUIPullRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.lua_button)
    QMUIButton luaButton;

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onInitCircle() {
        unbinder = ButterKnife.bind(this, getRootView());

        topBar.setTitle("DEMO OF K");
        refreshLayout.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() {
            @Override
            public void onMoveTarget(int offset) {

            }

            @Override
            public void onMoveRefreshView(int offset) {

            }

            @Override
            public void onRefresh() {
                refreshLayout.finishRefresh();
            }
        });


        listView.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listStrings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("name", listStrings[position]);
                startActivity(intent);
            }
        });

        luaButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),LuaDemoActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
